package com.inspectortime.webapp.inspector;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.inspectortime.domain.Inspection;
import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.domain.type.InspectionStatus;
import com.inspectortime.domain.util.InspectionDateComparator;
import com.inspectortime.repository.Repository;
import com.inspectortime.util.CalendarUtil;
import com.inspectortime.webapp.util.HttpSessionUtils;
import com.inspectortime.webapp.util.InspectionListNameDateComparator;

/**
 * Prepares data for the "Inspections" page
 * 
 * @author bones
 * 
 */
public class InspectionsController implements Controller {

	private Repository repository;
	private String returnView;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		InspectorCompany company = HttpSessionUtils.retrieveLoggedInCompany(
				request, repository);

		Calendar now = CalendarUtil.createDayCalendar();
		Calendar past = (Calendar) now.clone();
		past.add(Calendar.DATE, -10); // grab inspections from the past ten days

		List<Inspection> pastInspections = company.findInspectionsBetween(past
				.getTime(), now.getTime());
		List<Inspection> allFutureInspections = company.findInspectionsBetween(
				now.getTime(), null);

		// Sort the inspections
		if (StringUtils.hasText(request.getParameter("sort"))
				&& request.getParameter("sort").equals("inspector")) {
			InspectionListNameDateComparator comparator = new InspectionListNameDateComparator();
			Collections.sort(pastInspections, comparator);
			Collections.sort(allFutureInspections, comparator);

		} else {
			InspectionDateComparator comparator = new InspectionDateComparator();
			Collections.sort(pastInspections, comparator);
			Collections.sort(allFutureInspections, comparator);
		}

		// Now make a separate list for Quick Book Inspections and "Upcoming"
		// (non-quickbook) Inspections for easier display in the view
		List<Inspection> quickBookInspections = new ArrayList<Inspection>();
		for (Inspection inspection : allFutureInspections) {
			if (inspection.getInspectionStatus() != null
					&& inspection.getInspectionStatus().equals(
							InspectionStatus.QUICK_BOOK)) {
				quickBookInspections.add(inspection);
			}
		}

		List<Inspection> upcomingInspections = new ArrayList<Inspection>();
		for (Inspection inspection : allFutureInspections) {
			if (inspection.getInspectionStatus() != null
					&& inspection.getInspectionStatus() != InspectionStatus.INCOMPLETE) {
				if (!inspection.getInspectionStatus().equals(
						InspectionStatus.QUICK_BOOK)) {
					upcomingInspections.add(inspection);
				}
			}
		}

		ModelAndView mv = new ModelAndView(this.returnView);
		mv.addObject("inspectorCompany", company);
		mv.addObject("pastInspections", pastInspections);
		mv.addObject("allFutureInspections", allFutureInspections); // currently
																	// don't
																	// need this
																	// in the
																	// view, but
																	// keeping
																	// it just
																	// in case
		mv.addObject("quickBookInspections", quickBookInspections);
		mv.addObject("upcomingInspections", upcomingInspections);
		return mv;
	}

	public void setReturnView(String returnView) {
		this.returnView = returnView;
	}

	public void setRepository(Repository repository) {
		this.repository = repository;
	}

}
